<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/25
 * Time: 17:09
 */

namespace Home\Controller;


use Common\Utils\Constants;
use Home\Logic\GoodsLogic;
use Home\Logic\OrderLogic;

class GoodsController extends BaseController
{
    public function index(){
        $this->display();
    }

    /**
     * ajax获取商品列表
     */
    public function ajaxGoodsList(){
        $pagesize = 10;
        $pagenumber = I('pagenum', '1');
        $logic = new GoodsLogic();
        $data = $logic->getGoodsList(null,$pagenumber,$pagesize);
        $this->assign('data', $data);
        $this->display('ajaxGoodsList');
    }

    /**
     * ajax兑换商品
     */
    public function award(){
        $id = I('id');
        $user = $this->getUser();
        $logic = new GoodsLogic();
        $success = false;
        $result = $logic->award($id,$user['id']);
        $code = -1;
        $message = '';
        switch ($result){
            case 0:
                $message = '兑换失败！';
                break;
            case 1:
                $success = true;
                $message = '兑换成功，请联系客服领取！';
                break;
            case 2:
                $message = '对不起，您的钻石数目不足！';
                $code = Constants::CODE_INTEGRAL_INSUFFICIENT;
                break;
            case 3:
                $message = '对不起，该商品不存在！';
                break;
        }
        $this->ajax($success,$message,$code);
    }
}